package cc.ufinity.networkdiagnose.widget;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.Message;
import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.manager.MessageWhat;
import cc.ufinity.networkdiagnose.manager.Task;
import cc.ufinity.networkdiagnose.manager.TelManager;
import cc.ufinity.networkdiagnose.view.R;

public class RssiTestWidgetProxy extends GeneralTestWidget {

    private Activity ctx;
    private Handler handlerChild;
    private final Handler handler;  
    private Task task;
	public static int testFlag = 0; 
    public static int taskFlag = 0;  
	private TelManager tm;
	
	private boolean TEL_RSSI_SUCCESS = false;
	private boolean isFirst = true;
	
    public RssiTestWidgetProxy(Activity context, Handler handl) {
        super(context);
        this.ctx = context;
        this.handlerChild = handl;
        inflate(context, R.layout.webauth_test_widget_proxy, this); 
        task = (Task)ctx.getIntent().getSerializableExtra(TAG_REFER_TASK);  
        
        handler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case MessageWhat.DO_NEXT_TEST:
                    //showNextText();
                    break;   
                case MessageWhat.TEL_STATE_SUCCESS:
                	if(!TEL_RSSI_SUCCESS) {
                		TEL_RSSI_SUCCESS = true;
                		updateTel();
                	}
					break;
                }
            }
        };   
        tm = new TelManager(ctx);
		tm.setHandler(handler);
		/*new Thread(){
			public void run(){
				try {
					Thread.sleep(3000);
					updateTel();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}.start();*/
		
    } 
    
    private void updateTel() {
    	TEL_RSSI_SUCCESS=true;
    	if(tm!=null&&tm.getNetworkType().equals("HSDPA")){
        	GlobalApp.tdRrri=tm.getStrSignalStrength();
        	GlobalApp.tdCi=tm.getCI();
        	GlobalApp.tdLAc=tm.getLac();
        	System.err.println(GlobalApp.tdRrri+"------------------GlobalApp.tdLAc----------------"+"\n\n\n"+GlobalApp.tdCi
        			+"------------------GlobalApp.tdLAc--------------"+"\n\n\n"+GlobalApp.tdLAc);
    	}else {
    		GlobalApp.gsmTssi=tm.getStrSignalStrength();	
    		GlobalApp.GsmCi=tm.getCI();
        	GlobalApp.GsmLAc=tm.getLac();
        	System.err.println(GlobalApp.gsmTssi+"------------------GlobalApp.gsmLAc----------------"+"\n\n\n"+GlobalApp.GsmCi
        			+"------------------GlobalApp.gsmLAc--------------"+"\n\n\n"+        	GlobalApp.GsmLAc);
    	}
    	System.out.println(task.selNetType+"=======网络类型=========="+tm.getSignalStrength()+"======信号强度========"+task.rssi);
    	
    	
    	if(task.selNetType == ConnectivityManager.TYPE_MOBILE || task.selNetType == ConnectivityManager.TYPE_MOBILE_MMS)
        {
        	  task.rssi = String.valueOf(tm.getSignalStrength());
        	  System.out.println("TD网络 信号强度： -" + tm.getSignalStrength());
              //----------------------------------------------------------
        		if(isFirst) {
        			isFirst = false;
        			StringBuffer temp = new StringBuffer();
        			GlobalApp.result_SRRI_test = temp;
        		}
        		GlobalApp.result_SRRI_test.append("RSSI: " + task.rssi);
        	//----------------------------------------------------------
        }else { 
        	  System.out.println(task.selNetType+"==============================="+task.scanResult.level);
              task.rssi =String.valueOf( task.scanResult.level);
            //----------------------------------------------------------
      		if(isFirst) {
      			isFirst = false;
      			StringBuffer temp = new StringBuffer();
      			GlobalApp.result_SRRI_test = temp;
      		}
      		GlobalApp.result_SRRI_test.append("SSID: " + task.scanResult.SSID)
      				.append("\nMAC: " + task.scanResult.BSSID)
      				.append("\nRSSI: " + task.rssi);
      	//----------------------------------------------------------
		}
      

    	System.out.println("task.rssi => " + task.rssi);
    	
        //mSendBroadcast(ctx);
		handlerSendToTarget(handlerChild, MessageWhat.HANDLER_RSSI);
        ctx.getIntent().putExtra(TAG_REFER_TASK, task);
    }
 
}
